<?php
/**
 * Created by PhpStorm.
 * User: wenba
 * Date: 2019/11/13
 * Time: 16:56
 */



require './vendor/autoload.php';

use Services\HelloWorld\HelloWorldProcessor;
use Thrift\Exception\TException;
use Thrift\Factory\TTransportFactory;
use Thrift\Factory\TBinaryProtocolFactory;

use Thrift\Server\TServerSocket;
use Thrift\Server\TSimpleServer;


class HelloWorldHandler
{
    public function sayHello($name)
    {
        return 'hello ' . $name;
    }
}


$loader = new \Thrift\ClassLoader\ThriftClassLoader();
$loader->register();


try {

    $handler   = new HelloWorldHandler();
    $processor = new HelloWorldProcessor($handler);

    $transportFactory = new TTransportFactory();
    $protocolFactory  = new TBinaryProtocolFactory(true, true);

    //作为cli方式运行，监听端口，官方实现
    $transport = new TServerSocket('localhost', 9090);
    $server    = new TSimpleServer($processor, $transport, $transportFactory, $transportFactory, $protocolFactory, $protocolFactory);
    $server->serve();
} catch (TException $tx) {
    print 'TException: ' . $tx->getMessage() . "\n";
}
